/* 
 * @(#)ZipUtil.java    Created on 2012-7-2
 * Copyright (c) 2012 ZDSoft Networks, Inc. All rights reserved.
 * $Id$
 */
package bjxg.test.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

/**
 * @author chengcy
 * @version $Revision: 1.0 $, $Date: 2012-7-2 下午1:37:13 $
 */
public class ZipUtil {
    /**
     * 压缩字符串为 byte[] 储存可以使用new sun.misc.BASE64Encoder().encodeBuffer(byte[] b)方法 保存为字符串
     * 
     * @param str
     *            压缩前的文本
     * @return
     */
    public static byte[] compress(String str, String charsetName) throws IOException {
        byte[] input = str.getBytes(charsetName);

        // Create the compressor with highest level of compression
        Deflater compressor = new Deflater();
        compressor.setLevel(Deflater.DEFAULT_COMPRESSION);
        // Give the compressor the data to compress
        compressor.setInput(input);
        compressor.finish();

        // Create an expandable byte array to hold the compressed data.
        // You cannot use an array that's the same size as the orginal because
        // there is no guarantee that the compressed data will be smaller than
        // the uncompressed data.
        ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

        // Compress the data
        byte[] buf = new byte[1024];
        while (!compressor.finished()) {
            int count = compressor.deflate(buf);
            bos.write(buf, 0, count);
        }
        try {
            bos.close();
        }
        catch (IOException e) {
        }

        // Get the compressed data
        byte[] compressedData = bos.toByteArray();
        return compressedData;
    }

    /**
     * 将压缩后的 byte[] 数据解压缩
     * 
     * @param compressed
     *            压缩后的 byte[] 数据
     * @return 解压后的字符串
     */
    public static final String decompress(byte[] compressed, String charsetName) throws IOException {
        if (compressed == null)
            return null;

        // Create the decompressor and give it the data to compress
        Inflater decompressor = new Inflater();
        decompressor.setInput(compressed);

        // Create an expandable byte array to hold the decompressed data
        ByteArrayOutputStream bos = new ByteArrayOutputStream(compressed.length);

        // Decompress the data
        byte[] buf = new byte[1024];
        while (!decompressor.finished()) {
            try {
                int count = decompressor.inflate(buf);
                bos.write(buf, 0, count);
            }
            catch (DataFormatException e) {
            }
        }
        try {
            bos.close();
        }
        catch (IOException e) {
        }

        // Get the decompressed data
        byte[] decompressedData = bos.toByteArray();

        return new String(decompressedData, charsetName);
    }

    public static void main(String[] args) throws IOException {
        System.out.println(decompress(compress("测试", "utf-8"), "utf-8"));
    }
}
